<template>
  <el-container>
    <el-main class="main-bg">
      <el-form ref="searchForm" :model="searchForm" label-width="80px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="部门名称">
              <el-input
                  v-model="searchForm.unitName"
                  placeholder="请输入部门名称"
              />
            </el-form-item>
          </el-col>
          <el-col :offset="12" :span="6" class="text-right">
            <el-button
                icon="iconfont iconxianxing_sousuo"
                type="primary"
                @click="handleSearch"
            >
              查询
            </el-button>
            <el-button
                icon="iconfont iconxianxing_zhongzhi"
                @click="handleReset"
            >
              重置
            </el-button>
          </el-col>
          <el-col :span="24" class="text-left">
            <el-button type="primary" @click="onAdd">
              新增部门
            </el-button>
          </el-col>
        </el-row>
      </el-form>
      <Table
          ref="table"
          :current-page="page"
          :header-data="tableHeader"
          :index="false"
          :loading="loading"
          :page-size="limit"
          :table-data="tableData"
          :total="total"
          class="data-table"
          style="width: 100%"
          @now-page="handlePage"
          @now-size="handleSize"
          @table-sort="handleSort"
      />
    </el-main>
    <el-drawer :title="title" :visible.sync="drawerVisible">
      <div class="drawer-main">
        <el-form ref="form" :model="form" label-position="top" label-width="80px">
          <el-form-item label="部门名称">
            <el-input v-model="form.unitName" placeholder="请输入"/>
          </el-form-item>
          <!--          <el-form-item label="部门地址">-->
          <!--            <el-input v-model="form.unitAddress" placeholder="请输入"/>-->
          <!--          </el-form-item>-->
          <el-form-item label="备注">
            <el-input v-model="form.remark" placeholder="请输入" type="textarea"/>
          </el-form-item>
        </el-form>
      </div>
      <div v-if="type!=='look'" class="drawer-footer">
        <div class="footer-btn">
          <el-button type="primary" @click="handleSubmit"> 确认</el-button>
          <el-button @click="drawerVisible = false"> 取消</el-button>
        </div>
      </div>
    </el-drawer>
  </el-container>
</template>

<script>
import addTable from '@/mixins/addtable'
import { getList, postUnitDel, postUnitAdd, postUnitUpdate } from './api'

export default {
  name: 'UnitList',
  mixins: [addTable],
  data() {
    return {
      searchForm: {
        unitName: ''
      },
      title: '',
      type: 'look',
      tableHeader: [],
      personOptions: [],
      oneLoading: false,
      drawerVisible: false,
      form: {
        unitId: '',
        unitName: '',
        unitAddress: '',
        remark: ''
      }
    }
  },
  created() {
    this.tableHeader = [
      {
        prop: 'unitId',
        label: '编码',
        sortable: false,
        overflow: true,
        width: '180px',
        align: 'left'
      },
      {
        prop: 'unitName',
        label: '部门名称',
        sortable: false,
        overflow: true,
        width: 180,
        align: 'left'
      },
      // {
      //   prop: 'unitAddress',
      //   label: '部门地址',
      //   sortable: false,
      //   overflow: true,
      //   align: 'left'
      // },
      {
        prop: 'remark',
        label: '备注',
        sortable: false,
        overflow: true,
        align: 'left'
      },
      {
        width: 150,
        label: '操作',
        align: 'left',
        fixed: 'right',
        buttonList: (row, index) => {
          return [
            {
              text: '详情',
              type: 'text',
              event: this.onDetail
            },
            {
              text: '编辑',
              type: 'text',
              event: this.onEdit
            },
            {
              text: '删除',
              type: 'text',
              event: this.onDelete
            }
          ]
        }
      }
    ]
    this.getTableData()
  },

  methods: {
    onDetail(row) {
      this.form = {
        ...row
      }
      this.type = 'look'
      this.title = '查看详情'
      this.drawerVisible = true
    },
    onEdit(row) {
      this.form = {
        ...row
      }
      this.type = 'edit'
      this.title = '编辑部门'
      this.drawerVisible = true
    },
    onAdd() {
      this.form = {}
      this.type = 'add'
      this.title = '新增部门'
      this.drawerVisible = true
    },
    onDelete(row) {
      this.$confirm('您确认要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        postUnitDel({ unitId: row.unitId })
           .then(({ success, message }) => {
             if (success) {
               this.$message.success(message)
             } else {
               this.$message.error(message)
             }
             this.getTableData()
           })
           .catch()
      })
    },
    getTableData() {
      this.loading = true
      this.getTableParams()
      getList(this.tableParams)
         .then((res) => {
           if (!res) return
           const { data, pageInfo } = res
           setTimeout(() => {
             this.loading = false
             this.tableData = data
             this.total = pageInfo.total
           }, 500)
         })
         .catch((err) => {
           console.log('TCL: getTableData -> err', err)
         })
    },
    handleSubmit() {
      this.form.unitId
         ? postUnitUpdate(this.form).then(({ success, message }) => {
           if (success) {
             this.$message.success(message)
           } else {
             this.$message.error(message)
           }
           this.drawerVisible = false
           this.form = {}
           this.getTableData()
         })
         : postUnitAdd(this.form).then(({ success, message }) => {
           if (success) {
             this.$message.success(message)
           } else {
             this.$message.error(message)
           }
           this.drawerVisible = false
           this.form = {}
           this.getTableData()
         })
    }
  }
}
</script>
